package com.example.administrator.shzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 注册界面
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_back_register_activity)
    ImageView ivBack;//返回键
    @Bind(R.id.tv_login_register_activity)
    TextView tvLogin;//登陆
    @Bind(R.id.et_user_number_register_activity)
    EditText etUserNumber;//用户账号
    @Bind(R.id.et_get_auth_code_register_activity)
    EditText etGetAuthCode;//验证码
    @Bind(R.id.et_user_password_register_activity)
    EditText etUserPassword;//密码

    @Bind(R.id.iv_get_auth_code_register_activity)
    ImageView ivGetauthCode;

    @Bind(R.id.btn_register_register_activity)
    Button btnRegister;//注册
    @Bind(R.id.iv_show_password)
    ImageView ivShowPassword;

    private boolean isShowPassword=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);
        setListeners();

    }


    /**
     * 注册
     */
    private void register() {

        //手机号
        final String tel = etUserNumber.getText().toString().trim();
        //密码
        String password = etUserPassword.getText().toString().trim();

        String url = Contents.USER_REGISTER_URL;

        RequestParams params = new RequestParams(url);
        params.addBodyParameter("tel", tel);
        params.addBodyParameter("password", password);

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONObject object = new JSONObject(result);
                    int state = object.getInt("state");
                    if (state == 1) {
                        ToastUtils.showMessageShort(RegisterActivity.this, "注册成功！");
                        finish();
                    } else {
                        String msg = object.getString("msg");
                        ToastUtils.showMessageShort(RegisterActivity.this, "" + msg);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }

    /**
     * 监听事件
     */
    private void setListeners() {

        ivBack.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        ivGetauthCode.setOnClickListener(this);
        ivShowPassword.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        String password = etUserPassword.getText().toString().trim();
        String userNumber = etUserNumber.getText().toString().trim();
        String code = etGetAuthCode.getText().toString().trim();

        //正则表达式
        Pattern p = Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$");
        //判断用户的手机号码格式是否正确
        Matcher m = p.matcher(userNumber);
        boolean result = m.matches();

        switch (v.getId()) {


            case R.id.iv_back_register_activity://返回

                finish();

                break;


            case R.id.tv_login_register_activity://登陆

                startActivity(new Intent(this, LogInActivity.class));
                finish();

                break;


            case R.id.iv_get_auth_code_register_activity://获取验证码


                if (result) {
                    //获取验证码
                    getAuthCode();
                } else {
                    ToastUtils.showMessageShort(this, "请输入正确的手机号码");
                }


                break;


            case R.id.btn_register_register_activity://注册

                if (userNumber.equals("")) {

                    ToastUtils.showMessageShort(this, "用户名不能为空！");
                    return;
                } else if (password.equals("")) {

                    ToastUtils.showMessageShort(this, "密码不能为空！");
                    return;
                }

                if (result) {
                    register();
                } else {
                    ToastUtils.showMessageShort(this, "请输入正确的手机号！");
                }

                break;

            case R.id.iv_show_password:

                if(isShowPassword) {
                    etUserPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    isShowPassword = false;
                }else {
                    etUserPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    isShowPassword = true;
                }

                etUserPassword.postInvalidate();

                break;
        }
    }

    /**
     * 验证码
     */
    public void getAuthCode() {

        String tel = etUserNumber.getText().toString().trim();
        String url = Contents.USER_REGISTER_AUTH_CODE_URL + tel + "&type=0";
        RequestParams params = new RequestParams(url);

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONObject object = new JSONObject(result);
                    int state = object.getInt("state");
                    if (state == 1) {
                        //设置验证码
                        String code = (String) object.get("pwd");
                        etGetAuthCode.setText(code);
                    } else if (state == -1) {
                        ToastUtils.showMessageShort(RegisterActivity.this, "该手机号码已注册");
                    } else {
                        ToastUtils.showMessageShort(RegisterActivity.this, "验证码获取失败");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
